<template>
  <div class="recharge">
    <div>
      <van-nav-bar
        title="Recharge"
        left-arrow
        @click-left="$router.back()"
        right-text="Record"
        @click-right="$router.push('/rechargerecord')"
      />
    </div>
    <div class="recharge-content">
      <p>Amount:</p>
      <div class="pt-num">
        <span>₹</span>
        <input type="number" v-model="rechargeNum" maxlength="6" />
      </div>
      <div class="bd-line"></div>
      <p class="mgr">Recharge methods:</p>
      <div class="pt-check">
        <div class="rdo" v-for="(item, r) in radioList" :key="r">
          <input
            name="test"
            type="radio"
            :id="item.id"
            :value="item.id"
            v-model="checkedValue"
          />
          <label :for="item.id">{{ item.title }}</label>
        </div>
      </div>
      <div class="bd-line"></div>
      <div class="rege">
        <button @click="rechargeHandle">Recharge</button>
      </div>
    </div>
    <van-overlay :show="loading" @click="loading = false">
      <div class="loading" @click.stop>
        <van-loading color="#1989fa" type="spinner" size="30" />
      </div>
    </van-overlay>
  </div>
</template>

<script>
import { Money } from "@/utils/common";
export default {
  data() {
    return {
      rechargeNum: "",
      checkedValue: "",
      loading: false,
      radioList: [],
    };
  },
  mounted() {
    this.channels();
  },
  methods: {
    rechargeHandle() {
      console.log(this.checkedValue);
      if (this.rechargeNum > 0) {
        if (!this.checkedValue) {
          this.$toast("Please check the Recharge methods");
          return;
        }
        this.loading = true;
        this.$store
          .dispatch("recharge", {
            amount: parseInt(Money(this.rechargeNum)),
            payment_id: this.checkedValue,
          })
          .then((res) => {
            this.loading = false;
            if (res != false) {
              window.location.href = res.url;
            }
          })
          .catch((res) => {
            this.loading = false;
          });
      } else {
        this.loading = false;
        this.rechargeNum = "";
        this.$toast("Please enter the correct amount");
      }
    },
    channels() {
      this.$store
        .dispatch("channels")
        .then((res) => {
          this.radioList = res;
          this.checkedValue = res[0].id;
        })
        .catch((res) => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.recharge {
  min-height: 100vh;
}
.loading {
  width: 30px;
  height: 30px;
  margin: auto;
  margin-top: 300px;
}
.recharge-content {
  box-sizing: border-box;
  position: absolute;
  margin: 15px 0 0 0;
  padding: 20px 15px;
  width: 100%;
  height: calc(100% - 64px);
  background-color: #fff;
  p {
    font-size: 15px;
    color: #333333;
  }
  .pt-num {
    span {
      font-size: 50px;
    }
    input {
      height: 46px;
      width: 280px;
      padding: 0 10px;
      font-size: 50px;
      color: #4a67f5;
      border: none;
    }
  }
  .bd-line {
    height: 1px;
    background-color: #e2e2e2;
  }
  .pt-check {
    margin: 0 0 10px 0;
    .rdo {
      margin: 0 0 15px 0;
    }
    span {
      margin: 0 0 0 5px;
    }

    input[type="radio"] + label::before {
      content: " ";
      display: inline-block;
      vertical-align: middle;
      width: 25px;
      height: 25px;
      border-radius: 50%;
      border: 1.5px solid #4a67f5;
      margin-right: 5px;
      box-sizing: border-box;
    }
    input[type="radio"]:checked + label::before {
      background-color: #4a67f5;
      background-clip: content-box;
      padding: 3px;
      box-sizing: border-box;
    }
    input[type="radio"] {
      position: absolute;
      clip: rect(0, 0, 0, 0);
    }
    label {
      margin: 0 57px 0 0;
      font-size: 15px;
    }
  }
  .mgr {
    margin: 30px 0 15px 0;
  }
  button {
    width: 300px;
    height: 50px;
    background: #4a67f5;
    opacity: 1;
    border-radius: 105px;
    border: none;
  }
  .rege {
    margin: 30px auto;
    text-align: center;
    font-size: 17px;
    color: #fff;
  }
}
</style>